import { BaseDeployPanel } from "./BaseDeployPanel";
import { Language } from "../../../../../../extend/language/Language";
import { Cache } from "../../../../cache/Cache";
import { ItemData } from "../../../../resource/info/ItemData";
import { EquipUpdateParams } from "../../data/EquipUpdateParams";
import { EItemUpdateType } from "../../../../../../Message/Game/EItemUpdateType";
import { EquipmentUtil } from "../../../common/util/EquipmentUtil";
import { DataEvent } from "../../../../events/DataEvent";
import { EquipmentModuleConst } from "../../data/EquipmentModuleConst";
import { EBind } from "../../../../../../Message/Public/EBind";
import { ECategory } from "../../../../../../Message/Public/ECategory";
import { EquipAdvanceConfig } from "../../../../resource/EquipAdvanceConfig";
import { SPlayerItem } from "../../../../../../Message/Public/SPlayerItem";
import { EItemUpdateResult } from "../../../../../../Message/Game/EItemUpdateResult";
import { MsgManager } from "../../../../manager/MsgManager";
import { AdvanceResultPanel } from "../../popupPanels/AdvanceResultPanel";
import { PopupManager } from "../../../../manager/PopupManager";
import { GImageBitmap } from "../../../../../../com/mui/controls/GImageBitmap";
import { DeployPanelItem } from "../DeployPanelItem";
import { ObjCreate } from "../../../common/ObjCreate";
import { EquipPropButton } from "../EquipPropButton";
import { AdvanceEquipPanel } from "../AdvanceEquipPanel";
import { FilterConst } from "../../../../../component/gconst/FilterConst";
import { EPlayerItemPosType } from "../../../../../../Message/Public/EPlayerItemPosType";
import { Alert } from "../../../../../../com/mui/controls/Alert";
import { PulseSharedObject } from "../../../player/PulseSharedObject";
import { TodayNoTipsConst } from "../../../../../component/gconst/TodayNoTipsConst";
import { HTMLUtil } from "../../../../../../com/gengine/utils/HTMLUtil";
import { ShopConfig } from "../../../../resource/ShopConfig";
import { NetDispatcher } from "../../../../../mvc/core/NetDispatcher";
import { ServerCommand } from "../../../../mvc/ServerCommand";
import { TEquipAdvanceConfig } from "../../../../../../Message/Db/Tables/TEquipAdvanceConfig";
import { GCheckBox } from "../../../../../../com/mui/controls/GCheckBox";
import { _Label123 } from "../../../../../../this/_Label123";
type int = number;
//class EquipAdvancePanel
    
    export  class EquipAdvancePanel extends BaseDeployPanel
    {
        constructor()
        {
            this._Label123 = Language.getString(60162);
            this.posData = [{"x": 157, "y": 90, "label": Language.getString(60144), "canRemove": true, "noBoarder": false}, {"x": 18, "y": 150, "label": _Label123, "canRemove": false, "noBoarder": true}, {"x": 60, "y": 150, "label": _Label123, "canRemove": false, "noBoarder": true}, {"x": 102, "y": 150, "label": _Label123, "canRemove": false, "noBoarder": true}, {"x": 144, "y": 150, "label": _Label123, "canRemove": false, "noBoarder": true}, {"x": 186, "y": 150, "label": _Label123, "canRemove": false, "noBoarder": true}, {"x": 237, "y": 150, "label": _Label123, "canRemove": false, "noBoarder": true}, {"x": 295, "y": 150, "label":"", "canRemove": true, "noBoarder": true}];
            this.buttonLabel = Language.getString(60163);
            this._alarmText = Language.getString(60147);
            super();
            return;
        }

        /* internal  */notEnoughPack(): Boolean
        {
            if (!this._removeCb.selected || !allItem[0].itemData) 
            {
                return false;
            }
            var loc1=0;
            var loc2=allItem[0].itemData.itemExInfo;
            var loc3=1;
            while (loc3 <= 6) 
            {
                if (loc2["hole" + loc3] > 1) 
                {
                    loc1 = loc1 + 1;
                }
                ++loc3;
            }
            if (loc1 > Cache.instance.pack.backPackCache.getSpareCapacity()) 
            {
                return true;
            }
            return false;
        }

        /* internal  */onConfirmButtonClick(): void
        {
            var loc6=0;
            var loc1=this.currentEquipmentData.posType;
            var loc2=new Array<any>();
            var loc3="";
            if (allItem[0].itemData) 
            {
                loc3 = allItem[0].itemData.uid;
            }
            if (this._useItems) 
            {
                loc6 = 0;
                while (loc6 < this._useItems.length) 
                {
                    loc2.push((this._useItems[loc6] as ItemData).uid);
                    ++loc6;
                }
            }
            if (allItem[7].itemData) 
            {
                loc2.push(allItem[7].itemData.uid);
            }
            var loc4=allItem[7].itemData ? true : false;
            var loc5;
            (loc5 = new EquipUpdateParams()).operationId = this.operationId;
            loc5.autoUpdate = this._removeCb.selected;
            loc5.equipPosType = loc1;
            loc5.equipUid = loc3;
            loc5.materials = loc2;
            loc5.target = EItemUpdateType._EItemUpdateUpgrade;
            loc5.num = 0;
            loc5.flag = loc4;
            loc5.autoBuyTone = Cache.instance.equipment.isAutoBuy;
            EquipmentUtil.suitAttrUnlockAlert(allItem[0].itemData, false, this.dipatchAdvance, loc5);
            return;
        }

        /* internal  */dipatchAdvance(arg1: Object): void
        {
            var loc1=arg1 as EquipUpdateParams;
            this.dispatchEvent(new DataEvent(EquipmentModuleConst.Event_DoUpdateEvent1, loc1, false, true));
            return;
        }

        public moveOutByIndex(arg1: int): void
        {
            if (arg1 >= 0 && arg1 < allItem.length) 
            {
                if (arg1 != 0) 
                {
                    if (arg1 == 6) 
                    {
                        this.setAutoChooseLuckTune([]);
                        this.updateInfos();
                        updateStatus();
                    }
                }
                else 
                {
                    this.clear();
                }
            }
            return;
        }

        /* internal  */checkIsChangeToBind(): Boolean
        {
            var loc2=null;
            var loc1=false;
            var loc3=0;
            while (loc3 < this._useItems.length) 
            {
                loc2 = this._useItems[loc3] as ItemData;
                if (loc2.itemInfo.item.bind != EBind._EBindNo) 
                {
                    loc1 = true;
                }
                ++loc3;
            }
            if (allItem[0].itemData && !(allItem[0].itemData.itemExInfo.bind == EBind._EBindYes) && loc1) 
            {
                return true;
            }
            return false;
        }

        public /* override */ putInItem(arg1: ItemData, arg2: Boolean=false, arg3=null): void
        {
            clearAllItem();
            if (arg1.itemInfo.item.category == ECategory._ECategoryEquip) 
            {
                allItem[0].itemData = arg1;
                this.currentEquipmentData = arg1;
                this.currentEquipAdvanceConfig = EquipAdvanceConfig.instance.getAdvanceConfig(arg1);
                this.dispatchEvent(new DataEvent(EquipmentModuleConst.Event_ChoosedEquimentChange, [arg1], false, true));
                this.useProtectPropNum = 1;
            }
            this.updateInfos();
            updateStatus();
            return;
        }

        public /* override */ clear(): void
        {
            clearAllItem();
            this.currentEquipmentData = null;
            this.currentEquipAdvanceConfig = null;
            this._useItems = null;
            this.updateInfos();
            this.dispatchEvent(new DataEvent(EquipmentModuleConst.Event_ChoosedEquimentChange, [], false, true));
            return;
        }

        public /* override */ setAutoChooseMaterial(arg1: Object): void
        {
            var loc1=null;
            var loc2=null;
            var loc3=0;
            if (this.currentEquipmentData) 
            {
                this._useItems = arg1.useItems as Array<any>;
                loc1 = arg1.material as Array<any>;
                loc3 = 0;
                while (loc3 < loc1.length) 
                {
                    loc2 = loc1[loc3].data as ItemData;
                    allItem[loc3 + 1].itemData = loc2;
                    allItem[loc3 + 1].amount = loc2.itemAmount;
                    allItem[loc3 + 1].enabled = loc1[loc3].enough;
                    ++loc3;
                }
            }
            return;
        }

        public /* override */ setAutoChooseLuckTune(arg1: Object): void
        {
            var loc1=arg1.luckTune;
            if (loc1 && loc1.length > 0) 
            {
                allItem[7].itemData = loc1[0];
                this._useProtectPropNum = 1;
                this._useLukyPropBtn.visible = false;
            }
            else 
            {
                allItem[7].itemData = null;
                this._useProtectPropNum = 0;
                this._useLukyPropBtn.visible = true;
            }
            return;
        }

        /* internal  */updateInfos(): void
        {
            var loc3=0;
            var loc4=null;
            var loc5=null;
            var loc1=0;
            var loc2=allItem[0].itemData;
            if (loc2) 
            {
                loc3 = EquipAdvanceConfig.instance.getAdvancedEquipCode(loc2.itemCode).newItemCodeLevel;
                (loc4 = new SPlayerItem()).itemCode = loc3;
                loc4.itemAmount = loc2.data.itemAmount;
                loc4.jsStr = loc2.data.jsStr;
                loc5 = new ItemData(loc4);
                this._advanceEquipPanel.setEquip(loc5);
                if (this.currentEquipAdvanceConfig) 
                {
                    successRate = this.currentEquipAdvanceConfig.rate;
                    loc1 = this.currentEquipAdvanceConfig.cost;
                }
            }
            else 
            {
                this._advanceEquipPanel.setEquip(null);
                successRate = 0;
                loc1 = 0;
            }
            this.updatePosition();
            if (loc1 != _cost) 
            {
                _cost = loc1;
                dispatchEvent(new DataEvent(EquipmentModuleConst.Event_CostChange, _cost, false, true));
            }
            return;
        }

        public set useProtectPropNum(arg1: int)
        {
            this._useProtectPropNum = arg1;
            this.dispatchEvent(new DataEvent(EquipmentModuleConst.Event_LuckTuneNumChange, arg1, false, true, false));
            return;
        }

        /* internal  */updatePosition(): void
        {
            var loc1=0;
            var loc3=0;
            var loc4=0;
            var loc2=[];
            if (allItem[0].itemData) 
            {
                loc1 = 1;
                while (loc1 <= 6) 
                {
                    if (allItem[loc1].itemData) 
                    {
                        loc2.push(allItem[loc1]);
                        allItem[loc1].visible = true;
                    }
                    else 
                    {
                        allItem[loc1].visible = false;
                    }
                    ++loc1;
                }
            }
            else 
            {
                loc2 = [allItem[1], allItem[2], allItem[3]];
                allItem[4].visible = false;
                allItem[5].visible = false;
                allItem[6].visible = false;
            }
            loc2.push(allItem[7]);
            if (loc2.length < 6) 
            {
                loc3 = 175 - (38 * loc2.length + 16 * (loc2.length - 1)) / 2;
                loc4 = 54;
            }
            else if (loc2.length >= 6) 
            {
                loc3 = 25;
                loc4 = 45;
            }
            loc1 = 0;
            while (loc1 < loc2.length) 
            {
                loc2[loc1].x = loc3 + loc1 * loc4;
                loc2[loc1].visible = true;
                ++loc1;
            }
            this._useLukyPropBtn.x = allItem[7].x + 1;
            this._useLukyPropBtn.y = allItem[7].y + 1;
            return;
        }

        public /* override */ onUpdateResult(arg1: int, arg2: Boolean, arg3, arg4: Array<any>): void
        {
            var loc1=null;
            var loc2=null;
            if (arg1 != EItemUpdateResult._EItemUpdateResultSuccess) 
            {
                MsgManager.showRollTipsMsg(Language.getString(60172));
            }
            else 
            {
                loc1 = new ItemData(arg3);
                (loc2 = new AdvanceResultPanel()).setResultItem(loc1, this.operationId);
                PopupManager.addPopUp(loc2, "", true);
                MsgManager.showRollTipsMsg(Language.getString(60168));
                addUpdateSuccessEffect("equipEffect_equipAdvance.swf", 175, 122);
                if (allItem[0].itemData) 
                {
                    if (allItem[0].itemData.itemExInfo.strengthen > loc1.itemExInfo.strengthen && allItem[0].itemData.itemExInfo.prefix == loc1.itemExInfo.prefix) 
                    {
                        MsgManager.showRollTipsMsg(Language.getString(60169));
                    }
                    else if (allItem[0].itemData.itemExInfo.strengthen == loc1.itemExInfo.strengthen && allItem[0].itemData.itemExInfo.prefix > loc1.itemExInfo.prefix) 
                    {
                        MsgManager.showRollTipsMsg(Language.getString(60170));
                    }
                    else if (allItem[0].itemData.itemExInfo.strengthen > loc1.itemExInfo.strengthen && allItem[0].itemData.itemExInfo.prefix == loc1.itemExInfo.prefix) 
                    {
                        MsgManager.showRollTipsMsg(Language.getString(60171));
                    }
                }
            }
            this.clear();
            return;
        }

        protected /* override */ createChildren(): void
        {
            var loc3=null;
            var loc4=null;
            super.createChildren();
            var loc1=new GImageBitmap("equipment_successRate.png", 34, 68);
            successRateItem.addChild(loc1);
            successRateLabel.x = 53;
            successRateLabel.y = 92;
            this.addChild(successRateItem);
            var loc2=0;
            while (loc2 < this.posData.length) 
            {
                loc3 = this.posData[loc2];
                (loc4 = new DeployPanelItem(loc2, loc3.noBoarder)).posIndex = loc2;
                loc4.x = loc3["x"];
                loc4.y = loc3["y"];
                loc4.label = loc3["label"];
                if (loc3.canRemove) 
                {
                    loc4.mouseEnabled = true;
                    loc4.addEventListener(flash.events.MouseEvent.CLICK, this.onItemClick);
                }
                addChild(loc4);
                allItem.push(loc4);
                ++loc2;
            }
            this._removeCb = ObjCreate.createCheckBox(Language.getString(61035), 18, 311, 120, 20, this);
            this._useLukyPropBtn = new EquipPropButton(Language.getString(60126));
            this._useLukyPropBtn.x = allItem[7].x + 1;
            this._useLukyPropBtn.y = allItem[7].y + 1;
            this.addChild(this._useLukyPropBtn);
            this._useLukyPropBtn.addEventListener(flash.events.MouseEvent.CLICK, this.onButtonClick);
            this._advanceEquipPanel = new AdvanceEquipPanel();
            this._advanceEquipPanel.x = 73;
            this._advanceEquipPanel.y = 200;
            this.addChild(this._advanceEquipPanel);
            confirmButton.label = this.buttonLabel;
            confirmButton.x = 140;
            confirmButton.y = 306;
            confirmButton.textField.filters = [FilterConst.glowFilter];
            confirmButton.styleName = "ButtonSpecial";
            addChild(confirmButton);
            confirmButton.addEventListener(flash.events.MouseEvent.CLICK, this.onButtonClick);
            this.updatePosition();
            return;
        }

        public onItemClick(arg1: Object /* flash.events.MouseEvent */): void
        {
            var loc1=arg1.currentTarget as DeployPanelItem;
            this.moveOutByIndex(loc1.posIndex);
            return;
        }

        public onButtonClick(arg1: Object /* flash.events.MouseEvent */): void
        {
            this.onButtonClickImpl(arg1.currentTarget);
            return;
        }

        /* internal  */onButtonClickImpl(arg1): void
        {
            var target;
            var lossRate: Boolean;
            var hasShowAdvanceTip: Boolean;

            var loc1;
            hasShowAdvanceTip = false;
            target = arg1;
            if (!this.isCanAdvance(target)) 
            {
                return;
            }
            if (!(target == this._useLukyPropBtn) && allItem[0].itemData.posType == EPlayerItemPosType._EPlayerItemPosTypeRole && this._advanceEquipPanel.getEquip() && this._advanceEquipPanel.getEquip().itemInfo.item.level > Cache.instance.role.roleInfo.level) 
            {
                Alert.show("        " + Language.getString(60156));
                return;
            }
            lossRate = true;
            if (this.currentEquipAdvanceConfig && this.currentEquipAdvanceConfig.lossRateStrengthen == 0 && this.currentEquipAdvanceConfig.lossRatePrefix == 0) 
            {
                lossRate = false;
            }
            var loc2=target;
            switch (loc2) 
            {
                case confirmButton:
                {
                    hasShowAdvanceTip = PulseSharedObject.isTodayNotTips(TodayNoTipsConst.HasShowAdvanceTip);
                    if (!hasShowAdvanceTip && lossRate) 
                    {
                        PulseSharedObject.save(TodayNoTipsConst.HasShowAdvanceTip, true);
                        Alert.show(Language.getString(60164) + "\n" + HTMLUtil.addColor(Language.getString(60165), "#00FF00"), Language.getString(69900), Alert.OK | Alert.CANCEL, null, (arg1: int): void
                        {
                            if (arg1 == Alert.OK) 
                            {
                                doConfirmAdvance();
                            }
                            return;
                        })
                    }
                    else 
                    {
                        this.doConfirmAdvance();
                    }
                    break;
                }
                case this._useLukyPropBtn:
                {
                    if (lossRate) 
                    {
                        this.useProtectPropNum = 1;
                    }
                    else 
                    {
                        MsgManager.showRollTipsMsg(Language.getString(61055));
                    }
                    break;
                }
            }
            return;
        }

        /* internal  */doConfirmAdvance(): void
        {
            var loc1;
            if (this.checkIsChangeToBind()) 
            {
                Alert.show(this._alarmText, null, Alert.OK | Alert.CANCEL, null, (arg1: int): void
                {
                    if (arg1 == Alert.OK) 
                    {
                        onConfirmButtonClick();
                    }
                    return;
                })
            }
            else 
            {
                this.onConfirmButtonClick();
            }
            return;
        }

        /* internal  */isCanAdvance(arg1): Boolean
        {
            var loc1=null;
            var loc2=null;
            if (allItem[0].itemData == null) 
            {
                MsgManager.showRollTipsMsg(Language.getString(60148));
                return false;
            }
            var loc3=1;
            while (loc3 <= 6) 
            {
                if (allItem[loc3].itemData && allItem[loc3].enabled == false) 
                {
                    if (arg1 == confirmButton && Cache.instance.equipment.isAutoBuy) 
                    {
                        loc1 = allItem[loc3].itemData;
                        loc2 = ShopConfig.instance.getShopItemInfoByCode(loc1.itemCode);
                        if (loc2) 
                        {
                            return true;
                        }
                        this.showRollTips(loc3);
                    }
                    else 
                    {
                        this.showRollTips(loc3);
                    }
                    return false;
                }
                ++loc3;
            }
            if (!isEnoughMoney) 
            {
                MsgManager.showRollTipsMsg(Language.getString(60121));
                if (arg1 != this._useLukyPropBtn) 
                {
                    this.dispatchEvent(new DataEvent(EquipmentModuleConst.Event_CoinNotEnough, null, false, true, true));
                }
                return false;
            }
            if (this.notEnoughPack()) 
            {
                MsgManager.showRollTipsMsg(Language.getString(61031));
                return false;
            }
            return true;
        }

        /* internal  */showRollTips(arg1: int): void
        {
            if (allItem[arg1].itemData) 
            {
                MsgManager.showRollTipsMsg(Language.getStringByParam(60167, allItem[arg1].itemData.name));
            }
            else 
            {
                MsgManager.showRollTipsMsg(Language.getString(60166));
            }
            return;
        }

        /* internal  */onItemBuySuccess(): void
        {
            NetDispatcher.addCmdListener(ServerCommand.BackPackItemsChange, this.serverBagDataChangehandler);
            return;
        }

        /* internal  */serverBagDataChangehandler(arg1): void
        {
            NetDispatcher.removeCmdListener(ServerCommand.BackPackItemsChange, this.serverBagDataChangehandler);
            this.dispatchEvent(new DataEvent(EquipmentModuleConst.Event_ChoosedEquimentChange, [allItem[0].itemData], false, true));
            this.onButtonClickImpl(confirmButton);
            return;
        }

        /* internal */ /* var */currentEquipmentData: ItemData;

        /* internal */ /* var */currentEquipAdvanceConfig: TEquipAdvanceConfig;

        /* internal */ /* var */operationId: int=7;

        /* internal */ /* var */_useItems: Array<any>;

        /* internal */ /* var */_Label123: string;

        /* internal */ /* var */posData: Array<any>;

        /* internal */ /* var */_removeCb: GCheckBox;

        /* internal */ /* var */_useLukyPropBtn: EquipPropButton;

        /* internal */ /* var */buttonLabel: string;

        /* internal */ /* var */_useProtectPropNum: int=0;

        /* internal */ /* var */_alarmText: string;

        /* internal */ /* var */_advanceEquipPanel: AdvanceEquipPanel;
    }
